#!/bin/env python
#-*- coding:utf-8 -*-

#----------------------------------------------------------------------------
# Name     :      GUI.py
# Purpose  :      Mysql tool for ucweb, and this is the py file of gui.
# Author   :      Albert cheng
# Created  :      2012-10-29
# Copyright:     (c) 2012 by Albertcheng
# Licence  :      wxWindows license
#----------------------------------------------------------------------------

import wx
import string
from request_handler import request_sqlite 

class CharValidator(wx.PyValidator):
    def __init__(self, flag):
        
        wx.PyValidator.__init__(self)
        self.flag = flag
        self.Bind(wx.EVT_CHAR, self.OnChar)

    def Clone(self):
        """
        Note that every validator must implement the Clone() method.
        """
        return CharValidator(self.flag)

    def Validate(self, win):
        return True

    def TransferToWindow(self):
        return True 

    def TransferFromWindow(self):
        return True

    def OnChar(self, evt):
        try:
            key = chr(evt.GetKeyCode())
        except:
            return
        if self.flag == "no-alpha" and key in string.letters:
            return
        if self.flag == "no-digit" and key in string.digits:
            return
        if self.flag == "digit" and key not in string.digits:
            return
        if self.flag == "alpha" and key not in string.digits:
            return
        if self.flag == "ip" and (key not in string.digits and key != "."):
            return 
        evt.Skip()
        
class config_panel(wx.Panel):   
    def __init__(self,parent, log):
        self.panel = wx.Panel.__init__(self,parent)
        self.log = log
        ##=============================================
        # sizer definition
        
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        title_sizer = wx.BoxSizer(wx.HORIZONTAL)
        
        
        ##=============================================
        #　element definition　
        logo_png = wx.Image(r'./icon/Logo.png',wx.BITMAP_TYPE_PNG).Rescale(150,150).ConvertToBitmap()
        logo = wx.StaticBitmap(self, -1, logo_png, (0, 0))
        
        logo_txt = wx.StaticText(self,-1,"离子战锤工作室·数据操作工具\n".decode("utf-8"))
        font = wx.Font(21, wx.SWISS, wx.NORMAL, wx.NORMAL)
        logo_txt.SetFont(font)
        
        
        #　element definition
        
        button_def1 = [
                      [1,"数据库地址:".decode("utf-8"),"no-alpha", "host"],
                      [1,"数据库端口:".decode("utf-8"),"no-alpha", "port"],
                      [1,"用户名:".decode("utf-8"),"any",         "user"],
                      [1,"密码:".decode("utf-8"),"any",         "passwd"],
                      [2,"数据库默认编码:".decode("utf-8"),"any",   "charset"],
                      #[2,"天劫使用的数据库:".decode("utf-8"),"any",   "tj_db"],
                      #[2,"嵩鼠大战使用的数据库:".decode("utf-8"),"any",   "ssdz_db"],
                      ]
        
        
        self.fgs1 = wx.BoxSizer(wx.HORIZONTAL)
        self.fgs2 = wx.BoxSizer(wx.HORIZONTAL)
        
        for ndef in button_def1:
            nlabel = wx.StaticText(self, -1, ndef[1])
            
            nlabel.SetMinSize((100,20))
            nvalue = wx.TextCtrl(self, validator=CharValidator(ndef[2]))
            setattr(self, ndef[3], nvalue)
            
            nvalue.SetMinSize((110,20))
            
            
            getattr(self,"fgs"+str(ndef[0])).Add(nlabel, 0, wx.ALL|wx.RIGHT, 10)
            getattr(self,"fgs"+str(ndef[0])).Add(nvalue, 0, wx.EXPAND|wx.LEFT, 10)
        
   
        ##=============================================
        title_sizer.Add(logo,0,wx.ALL|wx.TOP,5)
        title_sizer.Add(logo_txt,0,wx.ALL|wx.CENTER,5)
        
        main_sizer.Add(title_sizer,0,wx.EXPAND,5)
        main_sizer.Add(wx.StaticLine(self, -1, (800,1)),0,wx.EXPAND,5)
        main_sizer.Add(self.fgs1,0,wx.ALL,5)
        main_sizer.Add(self.fgs2,0,wx.ALL,5)
        main_sizer.Add(wx.StaticLine(self, -1, (800,1)),0,wx.EXPAND,5)
        
        self.SetSizer(main_sizer)
        main_sizer.Fit(parent)
        
        
        
        
        self.sqlite_request = request_sqlite(self.log)
        self.cfg = self.sqlite_request.get()
        try:
            self.db_cfg = self.cfg[0][0]
        except:
            wx.MessageBox("读取配置数据错误，请确认配置文件完整！".decode("utf-8"), "Fatal Error", wx.OK)
            self.db_cfg = ("Null","Null","Null","Null","Null","Null","Null")
        
        self.host.SetValue(str(self.db_cfg[2]))
        self.port.SetValue(str(self.db_cfg[3]))
        self.user.SetValue(str(self.db_cfg[4]))
        self.passwd.SetValue(str(self.db_cfg[5]))
        self.charset.SetValue(str(self.db_cfg[6]))
        
        
        
        